package com.leyou.item.controller;

import com.leyou.common.domain.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import com.leyou.dto.BrandDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/04 12:01
 * @description:
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

	@Autowired
	private BrandService brandService;
	@DateTimeFormat
	private Date date;

	/**
	 * @param page   查询的当前页数
	 * @param rows   一页显示的数据条数
	 * @param key    查询的内容
	 * @param sortBy 根据哪个字段排序
	 * @param desc   升序还是降序
	 * @return
	 */
	@GetMapping("/page")
	public ResponseEntity<PageResult<BrandDto>> pageQuery(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "5") Integer rows,
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "sortBy", required = false) String sortBy,
			@RequestParam(value = "desc", defaultValue = "false") Boolean desc
	) {
		PageResult<BrandDto> pageResult = brandService.pageQuery(page, rows, key, sortBy, desc);
		return ResponseEntity.ok().body(pageResult);
	}

	@PostMapping
	public ResponseEntity<Void> addBrand(BrandDto brandDto, @RequestParam("cids") List<Integer> cids) {
		brandService.addBrand(brandDto, cids);
		return ResponseEntity.status(HttpStatus.CREATED).build();
	}

	@PutMapping
	public ResponseEntity<Void> editBrand(BrandDto brandDto, @RequestParam("cids") List<Integer> cids) {
		brandService.editBrand(brandDto,cids);
		return ResponseEntity.status(HttpStatus.OK).build();
	}

	@DeleteMapping("{id}")
	public ResponseEntity<Void> delBrand(@PathVariable("id") Long bid){
		brandService.delBrand(bid);
		return ResponseEntity.ok().build();
	}

	@GetMapping("/of/category")
	public ResponseEntity<List<BrandDto>> queryBrandByCid(@RequestParam("id") Long cid){
		List<BrandDto> brandDtoList = brandService.queryByCid(cid);
		return ResponseEntity.ok(brandDtoList);
	}


	@GetMapping("/of/{id}")
	public ResponseEntity<BrandDto> queryBrandByBranId(@PathVariable("id")Long brandId){
		BrandDto brandDto = brandService.queryById(brandId);
		return ResponseEntity.ok(brandDto);
	}

	@GetMapping("/list")
	public ResponseEntity<List<BrandDto>> queryBrandsByBrandIds(@RequestParam("bids")List<Long> bids){
		List<BrandDto> brandDtoList = brandService.queryBrandByIds(bids);
		return ResponseEntity.ok(brandDtoList);
	}


}
